const app = getApp();

const _http =(method, urlTail, data, response, error,retryCount) =>{
    const reqData = {body:data}
    let loginToken = '';
    if(app.globalData.userInfo){
        loginToken = app.globalData.userInfo.loginToken
    }
    const url = app.globalData.httpDomain + urlTail;
    console.log("url = " +url+ " authorization = " + loginToken );
    console.log(reqData);
    wx.request({
        method: method,
        url: url,
        data: reqData,
        header: {
            'content-type': 'application/json',
            'authorization': loginToken,
        },
        success: res =>{
            console.log(res)
            if(res.data.code == 198 && retryCount < 3){
                // 登录
                _login(()=>{
                    retryCount = retryCount + 1;
                   _http(method, urlTail, data, response, error, retryCount)
                },(res)=>{
                  if(error){return error(res)}
                })
            }else{
                return response(res)
            }
        },
        fail:err =>{
            if(error){
                return error(err)
            }
        },
        complete:info =>{}
    });
}

const _login =(response, error) => {
    wx.login({
        success: res => {
            var url = app.globalData.httpDomain + _url.wxLogin;
            var reqData = {
                code: res.code,
                appId: wx.getAccountInfoSync().miniProgram.appId
            }
            console.log(url);
            console.log(reqData);
            wx.request({
                method: 'POST',
                url:url,
                data: {
                    body:reqData
                },
                success: res =>{
                    console.log(res)
                    if(res.data.code == 200){
                        app.globalData.userInfo = res.data.body;
                        return response(res)
                    }else{
                        if(error){
                            return error(res)
                        }
                    }
                },
                fail:err =>{
                    console.log(err);
                    if(error){
                        return error(err)
                    }
                },
            })
        },
        fail: res => {
            console.log(res);
        }
    });
}

const _url = {
    /**登录注册 */
    wxLogin:'/xcx/v1/wx/login/check.do'
    ,registerUserInfo:'/xcx/v1/wx/info/register.do'
    ,registerPhoneUserInfo:'/xcx/v1/wx/phone/register.do'
    ,userImgUp:'/xcx/v1/member/img/upload.do'  //上传用户图片
    ,userInfoUp:'/xcx/v1/member/info/update.do'//上传用户信息

    /**充电插座 */
    ,cdczScan:'/xcx/v1/cdcz/scan/get.do'               //获取设备信息
    ,payMeal:'/xcx/v1/cdcz/set_meal/pay.do'            //套餐支付
    ,cdczpay:'/xcx/v1/cdcz/charge_power/pay.do'        //充满即停支付
    ,cdczResult:'/xcx/v1/cdcz/pay/result.do'           //支付结果
    ,cdczClose:'/xcx/v1/cdcz/order/close.do'           //订单完成
    ,cdczWorking:'/xcx/v1/cdcz/order/working/get.do'  //工作中订单
    /**共享充电宝 */
    ,cdbScan:'/xcx/v1/cdb/scan/get.do'             //获取设备信息
    ,cdbPay:'/xcx/v1/cdb/lease/pay.do'             //充电宝支付
    ,cdbResult:'/xcx/v1/cdb/pay/result.do'         //支付结果
    /**便捷线充 */
    ,pclScan:'/xcx/v1/pcl/scan/get.do'             //获取设备信息
    ,pclPay:'/xcx/v1/pcl/lease/pay.do'             //支付
    ,pclResult:'/xcx/v1/pcl/pay/result.do'         //支付结果
    ,pclChangePwd:'/xcx/v1/pcl/pwd/change.do'         //切换密码
     /**便携式储能电池 */
    ,bxscnBatteryType:'/xcx/v1/bxscn/battery_type/find.data'      //储能电池类型列表
    ,bxscnBatteryInfo:'/xcx/v1/bxscn/battery_type/info.data'           //储能电池类型的租借信息
    ,bxscnBatteryPrice:'/xcx/v1/bxscn/battery_type/price.data'         //储能电池类型的租金
    ,bxscnBatteryShop:'/xcx/v1/bxscn/battery_type/shop/find.data'      //储能电池租借的网点列表
    ,bxscnBatteryPay:'/xcx/v1/bxscn/shop/lease/pay.do'                  //到店自提预约租借
    ,bxscnBatteryPayResult:'/xcx/v1/bxscn/shop/pay/result.do'

    ,bxscnBatteryOrderInfo:'/xcx/v1/member/bxscn_shop/order/info.data'       //电池门店订单详情
    ,bxscnBatteryOrderRevert:'/xcx/v1/member/bxscn_shop/order/revert.do'     //到店自提预约归还

    ,bxscnScan:'/xcx/v1/bxscn/scan/get.do'             //获取设备信息
    ,bxscnPay:'/xcx/v1/bxscn/lease/pay.do'             //便携式电池租借支付
    ,bxscnPayResult:'/xcx/v1/bxscn/pay/result.do'      //支付结果
    ,bxscnOpenSlot:'/xcx/v1/bxscn/slot/open.do'       //归还开仓
    ,bxscnOpenSlotResult:'/xcx/v1/bxscn/slot/open/result.do'   //归还开仓结果

    /**订单 */
    ,orderList:'/xcx/v1/member/order/list.data'        //订单列表
    ,orderInfo:'/xcx/v1/member/order/info.data'        //订单详情
    
    /**钱包 */
    ,walletGet:'/xcx/v1/member/wallet/get.do'       //钱包信息
    ,walletAdd:'/xcx/v1/member/wallet/recharge.do'  //钱包充值
    ,walletGetOut:'/xcx/v1/member/wallet/transfer_money/apply.do' //钱包提现
    ,walletGetList:'/xcx/v1/member/wallet/transfer_money/order/list.data' //钱包提现列表
    ,walletGetInfo:'/xcx/v1/member/wallet/transfer_money/order/info.data' //钱包提现详情

    /* 优惠券 */
    ,couponList:'/xcx/v1/member/coupon/list.data'     //优惠券列表
    ,couponInfo:'/xcx/v1/coupon_activity/scan/get.do' //优惠券信息
    ,couponGet:'/xcx/v1/coupon_activity/receive.do'   //优惠券获取

    /* 代理端 */
    ,agentBind:'/xcx/v1/member/agent/bind.do' //绑定账号

    /* 常用设备 */
    ,useDeviceList:'/xcx/v1/member/common_device/list.data' //常用设备列表
    ,useDeviceAdd:'/xcx/v1/member/common_device/add.do'     //常用设备添加
    ,useDeviceDel:'/xcx/v1/member/common_device/del.do'     //常用设备删除

    /* 通用 */
    ,commonSMS:'/xcx/v1/common/sendSMS.do' //发送短信
    ,commonNearPlace:'/xcx/v1/common/nearby_place/list.data' //附件网点
    ,commonPlaceRecommend:'/xcx/v1/common/place_recommendation/apply.do' //推荐场地
    ,commonAppConfig:'/xcx/v1/common/app_config/get.data' //获取应用配置
    ,agreementInfo:'/xcx/v1/common/agreement/get.data'
}

const VFcode = app.globalData.httpDomain+'/xcx/v1/common/verifyCode.jpeg';


module.exports = {
  url:_url,
  getVFcode:VFcode,
  //请求方式、请求地址、请求参数、回调函数、错误处理
  get:(url,data,response,error) => _http('GET',url,data,response,error,0),
  post:(url,data,response,error) => _http('POST',url,data,response,error,0),
  login:(response,error)=>_login(response,error),
}

